package ar.edu.itba.it.cg.grupo01.impl;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

import javax.vecmath.Point3d;

import ar.edu.itba.it.cg.grupo01.api.Intersection;
import ar.edu.itba.it.cg.grupo01.api.shape.Shape;

public class IntersectionImpl implements Intersection<Shape> {

	private Set<Point3d> points;
	private Shape object;

	public IntersectionImpl(Collection<Point3d> points) {
		this.points = new HashSet<Point3d>(points);
	}

	public IntersectionImpl(Set<Intersection<? extends Shape>> intersections) {
		this.points = new HashSet<Point3d>();
		for (Intersection<? extends Shape> intersection : intersections) {
			this.points.addAll(intersection.getIntersections());
		}
	}

	@Override
	public Set<Point3d> getIntersections() {
		return points;
	}

	@Override
	public boolean intersects() {
		return !points.isEmpty();
	}

	@Override
	public Shape getObject() {
		return object;
	}

	@Override
	public double distance(Point3d point) {

		double distance = Double.MAX_VALUE;
		for (Point3d intersections : points) {
			double currentDistance = intersections.distance(point);

			if (currentDistance < distance) {
				distance = currentDistance;
			}
		}

		return distance;
	}

	@Override
	public Point3d closestTo(Point3d origin) {
		
		Point3d answer = null;
		double currentDistance = Double.MAX_VALUE;
		
		for (Point3d point : points) {
			double distance = point.distance(origin);
			
			if( distance < currentDistance ) {
				currentDistance = distance;
				answer = point;
			}
		}
		
		
		return answer == null ? null : (Point3d) answer.clone();
	}











}
